package com.glory.dmegcpda.viewmodel

import com.glory.dmegcpda.adapter.KeepInfoAdapter
import com.glory.dmegcpda.adapter.PartAdapter
import com.glory.dmegcpda.bean.KeepInfoEntity
import com.glory.dmegcpda.model.KeepRepository
import com.glory.mvvmnet.base.BaseViewModel
import com.glory.mvvmnet.entity.ToastMessageBean
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DateUtils

/**
 * FileName: CheckInfoViewModel
 * Author: XJH
 * Date: 2021/10/21 16:18
 * Description:
 * History:
 */
class KeepInfoViewModel : BaseViewModel() {
    val keepRepository by lazy { KeepRepository() }
    val keepListTitle = arrayListOf("序号","内容","完成","备注")
    val partListTitle = arrayListOf("序号","零件编号","数量","备注")
    val keepList = arrayListOf<KeepInfoEntity.PmsSheetDataEntity.CheckDataEntity>()
    val partList = arrayListOf<KeepInfoEntity.PmsSheetDataEntity.PartDataEntity>()
    val dataList = arrayListOf<KeepInfoEntity.PmsSheetDataEntity.DataLineEntity>()
    val mKeepAdapter : KeepInfoAdapter by lazy { KeepInfoAdapter(keepList) }
    val mPartAdapter : PartAdapter by lazy { PartAdapter(partList) }

    fun init(sheetRRN : String){
        val map = JsonObject()
        map.addProperty("ACTIONTYPE","GETDATA")
        map.addProperty("SCHEDULERRN",sheetRRN)
        launchOnlyResult({
            val keepInfo = keepRepository.getKeepInfo(map)
            keepList.addAll(keepInfo.PMSSHEETDATA.CHECKLINEDATALIST)
            partList.addAll(keepInfo.PMSSHEETDATA.PARTSLINEDATALIST)
            dataList.addAll(keepInfo.PMSSHEETDATA.DATASLINEDATALIST)
            mKeepAdapter.notifyDataSetChanged()
            mPartAdapter.notifyDataSetChanged()
        })
    }

    fun save(sheetRRN : String,keepPerson : String,isTemp : String){
        val map = JsonObject()
        map.addProperty("ACTIONTYPE","FORM")
        map.addProperty("ISFINISHED",isTemp)
        map.addProperty("SCHEDULERRN",sheetRRN)
        map.addProperty("OPERATOR",keepPerson)
        map.add(
            "CHECKLINEDATALIST",
            JsonParser().parse(Gson().toJson(mKeepAdapter.data)).asJsonArray
        )
        val iterator = mPartAdapter.data.iterator()
        while (iterator.hasNext()){
            val next = iterator.next()
            val partSuseEntity = KeepInfoEntity.PmsSheetDataEntity.PartSuseEntity(next.PARTSNAME,next.PARTSNAME,
                FreeApi_DateUtils.getNowStringDate("yyyy/MM/dd HH:mm:ss"),"",next.USEDQTY)
            next.PARTSUSELIST.add(partSuseEntity)
        }
        map.add(
            "PARTLINEDATALIST",
            JsonParser().parse(Gson().toJson(mPartAdapter.data)).asJsonArray
        )
        map.add(
            "DATALINEDATALIST",
            JsonParser().parse(Gson().toJson(dataList)).asJsonArray
        )
        launchOnlyResult({
            keepRepository.saveKeepInfo(map)
            defUI.toastEvent.value = ToastMessageBean("保存成功",true)
            defUI.startActivity.call()
        })
    }

}